﻿// <copyright file="AreaPropertyEditor.cs" company="City of Portland, BES-ASM">
// </copyright>
// <summary>AreaPropertyEditor Form</summary>

namespace BESASM.EMGAATS.UI
{
  using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows.Forms;
  using BESASM.EMGAATS.BusinessClasses;

  /// <summary>
  /// A form for editing area properties.
  /// </summary>
  public partial class AreaPropertyEditor : Form
  {
    /// <summary>
    /// Initializes a new instance of the <see cref="AreaPropertyEditor"/> class.
    /// </summary>
    public AreaPropertyEditor()
    {
      this.InitializeComponent();

      foreach (var areaType in Enum.GetValues(typeof(AreaType)))
      {
        this.areaTypeComboBox.Items.Add(areaType);
      }
    }

    /// <summary>
    /// Gets or sets the area to be edited.
    /// </summary>
    public IArea Area
    {
      get
      {
        return this.areaBindingSource.DataSource as IArea;
      }

      set
      {
        this.areaBindingSource.DataSource = null;

        this.directorsListBox.Items.Clear();
        this.overlandNodeTextBox.Text = "<Null>";

        if (value != null)
        {
          foreach (IDirector director in value.Directors)
          {
            this.directorsListBox.Items.Add(new DirectorWrapper(director));
          }

          if (value.OverlandFlowDestination != null)
          {
            this.overlandNodeTextBox.Text = value.OverlandFlowDestination.NodeName;
          }
        }

        this.areaBindingSource.DataSource = value;
      }
    }

    private void DirectorsListBox_DoubleClick(object sender, EventArgs e)
    {
      DirectorWrapper selectedItem = this.directorsListBox.SelectedItem as DirectorWrapper;
      if (selectedItem != null)
      {
        DirectorPropertyEditor directorEditor = new DirectorPropertyEditor() { Director = selectedItem.Director };
        directorEditor.Show(this);
      }
    }

    private void CancelButton_Click(object sender, EventArgs e)
    {
      this.Close();
    }
  }
}
